package com.qiwenshare.file.controller;

import org.springframework.stereotype.Component;

import java.sql.*;

@Component
public class JdbcUntils {
    private static String  driver = "com.mysql.cj.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/day16?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true";
    private static String user = "root";
    private static String pass = "root";

    //注册驱动
    static {
        try {
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取连接
    public static Connection getJdbc() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user, pass);
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }


    //关闭连接
    public static void close(Connection  conn, Statement statement, ResultSet resultSet) throws Exception {

        if (resultSet != null){
            resultSet.close();
        }
        if (statement != null){
            statement.close();
        }
        if (conn !=null){
            conn.close();
        }
    }
}
